// 暴力求解

class Solution {
public:
    int canCompleteCircuit(vector<int>& gas, vector<int>& cost) {
        int n = gas.size();
        for(int i = 0; i < n; ++i)
        {
            if(gas[i] < cost[i]) continue;
            int curGas = gas[i]; // 4
            int begin = i; // 2
            int cnt = 0;
            while(curGas >= 0) 
            {
                if(curGas < cost[begin % n]) break;
                curGas = curGas - cost[begin % n] + gas[(begin + 1) % n]; // 3 
                ++begin; // 4
                ++cnt;
                if((begin % n) == i)
                {
                    return i;
                }
            }
            if(curGas == 0 && (begin % n) == i)
            {
                return i;
            }
            else
            {
                i = i + cnt;
            }
        }
        return -1;
    }
};
